from types import FunctionType

from src.allview.allviews import AllViews
from src.decoators.class_decorator import singleton_decorator


@singleton_decorator
class ErrorMsg(object):
    def __init__(self):
        # 初始化错误信息字典
        self.ERROR_MSG = {k: {"status": False, "reason": "未执行"} for k in
                          [fun for fun, t in AllViews.__dict__.items() if
                           type(t) == FunctionType and not fun.startswith("_") and not fun.__contains__("decorator")]}

    @property
    def error_msg(self):
        error_msg_list = [
            "{func_name}:{status},原因:{reason}".format(func_name=self.format_func_name(func_name),
                                                      status="成功" if dic["status"] else "失败",
                                                      reason=dic["reason"] if not dic["status"] else "无")
            for func_name, dic in self.ERROR_MSG.items()
        ]
        return "\n".join(error_msg_list)

    @error_msg.setter
    def error_msg(self, kwargs):
        self.ERROR_MSG = {**self.ERROR_MSG, **kwargs}

    @classmethod
    def format_func_name(cls, func_name):
        return {
            "add_action": "添加活动",
            "add_sales_rmd": "添加营销政策",
            "wp_params_save": "添加参数",
            "wp_batch_save": "新增批次",
            "wp_record_save": "新增反馈定义",
            "verify_depend_rc_id": "验证反馈定义依赖参数名",
        }.get(func_name)
